package com.dhms.vostok1.model

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.dhms.vostok1.data.message_center.ReceiveMessageData
import com.dhms.vostok1.network.MoshiObject
import com.dhms.vostok1.utils.MessageCenterWebSocket
import com.dhms.vostok1.utils.NetworkState
import com.squareup.moshi.JsonAdapter
import kotlinx.coroutines.launch

/*离线模式状态*/
enum class OfflineModeState {
    //无
    NONE,

    //离线模式
    OFFLINE,

    //数据同步中
    SYNCHRONIZING,

    //数据同步完成
    COMPLETE,

    //数据同步错误
    ERROR
}

class WebSocketViewModel : ViewModel() {
    private var webSocket: MessageCenterWebSocket? = null

    private val _receiveMessage = MutableLiveData<ReceiveMessageData>()
    val receiveMessage: LiveData<ReceiveMessageData> = _receiveMessage

    private val _networkChange = MutableLiveData<NetworkState>()
    val networkChange: LiveData<NetworkState> = _networkChange

    //离线模式状态的变化
    private var _offlineModeState =
        MutableLiveData<OfflineModeState>(OfflineModeState.NONE)
    val offlineModeState: LiveData<OfflineModeState> = _offlineModeState

    private var _syncDataContent =
        MutableLiveData<String>("")
    val syncDataContent: LiveData<String> = _syncDataContent

    fun setOfflineModeState(state: OfflineModeState) {
        viewModelScope.launch {
            _offlineModeState.value = state
        }
    }

    fun setSyncDataContent(content: String) {
        viewModelScope.launch {
            _syncDataContent.value = content
        }
    }

    fun createWebSocket() {
        if (webSocket != null) {
            webSocket?.reconnectSocket()
            Log.i(TAG, "已经有存在的WebSocket")
            return
        }

        webSocket = MessageCenterWebSocket(
            { message ->
                val jsonAdapter: JsonAdapter<ReceiveMessageData> =
                    MoshiObject.moshi.adapter<ReceiveMessageData>(ReceiveMessageData::class.java)
                jsonAdapter.fromJson(message)?.let {
                    viewModelScope.launch {
                        _receiveMessage.value = it
                    }
                }
            }, {
                viewModelScope.launch {
                    _networkChange.value = it
                }
            })
        webSocket?.createWebSocket()
    }

    fun closeWebSocket() {
        webSocket?.closeWebSocket()
        webSocket = null
    }

    /*
    //TODO: 测试用，后边删掉
    private var timer: Timer? = null
    fun demoModeStateChange() {
        timer = Timer()
        timer?.schedule(object : TimerTask() {
            override fun run() {
                viewModelScope.launch {
                    when (offlineModeState.value) {
                        OfflineModeState.NONE -> {
                            _offlineModeState.value = OfflineModeState.OFFLINE
                        }
                        OfflineModeState.OFFLINE -> {
                            _offlineModeState.value = OfflineModeState.SYNCHRONIZING
                        }
                        OfflineModeState.SYNCHRONIZING -> {
                            _offlineModeState.value = OfflineModeState.COMPLETE
                        }
                        OfflineModeState.COMPLETE -> {
                            _offlineModeState.value = OfflineModeState.ERROR
                        }
                        OfflineModeState.ERROR -> {
                            _offlineModeState.value = OfflineModeState.NONE
                        }
                        null -> TODO()
                    }
                }
            }
        }, 0, 3000)
    }*/

    companion object {
        @JvmField
        val instance: WebSocketViewModel = WebSocketViewModel()
        const val TAG = "WebSocketViewModel"
    }
}
